package com.coding.service.impl;

import com.coding.dto.RestBean;
import com.coding.entity.UserFollowersVo;
import com.coding.mapper.UserFollowers0Mapper;
import com.coding.service.UserFollowers0Service;
import com.coding.util.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserFollowers0ServiceImpl implements UserFollowers0Service {

    @Resource
    private UserFollowers0Mapper userFollowers0Mapper;
    //查询用户关注的列表
    @Override
    public RestBean<List<UserFollowersVo>> queryAllFollower(String id) {
        List<UserFollowersVo> userFollowersVos = userFollowers0Mapper.queryAllFollower(id);
        System.out.println(userFollowersVos);
        return RestBean.success("查询成功", userFollowersVos);
    }
    //取消关注
    @Override
    public RestBean<Long>  updateStatus(String id) {
        int i = userFollowers0Mapper.updateStatus(id);
        System.out.println(i);
        if (i>0){
            return RestBean.success("取消关注成功");
        }
        return RestBean.failure(400,"取消关注失败");
    }
    //查询and模糊查询
    @Override
    public RestBean<List<UserFollowersVo>>  blurName(String id,String userName) {
        List<UserFollowersVo> userFollowersVos = userFollowers0Mapper.blurName(id,userName);
        System.out.println(userFollowersVos);
        return RestBean.success("查询成功", userFollowersVos);
    }

    @Override
    public RestBean<List<UserFollowersVo>>  queryAllContacts(String id) {
        List<UserFollowersVo> userFollowersVos = userFollowers0Mapper.queryAllContacts(id);
        System.out.println(userFollowersVos);
        return RestBean.success("查询成功", userFollowersVos);
    }
    //互为朋友查询and模糊查询
    @Override
    public RestBean<List<UserFollowersVo>>  ContactsName(String id,String userName) {
        List<UserFollowersVo> userFollowersVos = userFollowers0Mapper.ContactsName(id,userName);
        System.out.println(userFollowersVos);
        return RestBean.success("查询成功", userFollowersVos);
    }
    //查询所有的粉丝
    @Override
    public RestBean<List<UserFollowersVo>>  queryAllFans(String id) {
        List<UserFollowersVo> userFollowersVos = userFollowers0Mapper.queryAllFans(id);
        System.out.println(userFollowersVos);
        return RestBean.success("查询成功", userFollowersVos);
    }
    // 粉丝查询and模糊查询
    @Override
    public RestBean<List<UserFollowersVo>>  fuzzyName(String id, String userName) {
        List<UserFollowersVo> userFollowersVos = userFollowers0Mapper.fuzzyName(id, userName);
        System.out.println(userFollowersVos);
        return RestBean.success("查询成功", userFollowersVos);
    }
    //回关
    @Override
    public RestBean<String>  insertFriend(String followeeId) {
        // 查询用户 ID 和关注者 ID
        Map<String, Object> ids = userFollowers0Mapper.selectUserIds(followeeId);
        log.info("查询结果：" + ids);

        // 判断 ids 是否为空或者没有记录
        if (ids == null || ids.isEmpty()) {
            log.info("未找到相关记录");
            return RestBean.failure(400,"未找到相关记录");
        }

        // 获取 user_id 和 followee_id 的值
        String userId = String.valueOf(ids.get("user_id"));
        String followeeIdLong = String.valueOf(ids.get("followee_id"));

        // 插入朋友关系记录
        int insertResult = userFollowers0Mapper.insertContact(userId, followeeIdLong);
        if (insertResult > 0) {
            return RestBean.success("插入成功");
        } else {
            return RestBean.failure(400,"插入失败");
        }
    }
}
